<script setup lang="ts">
import useAppState from "~/hooks/useAppState";
import PcMenu from "./PcMenu.vue";

defineProps<{
  navList: {
    name: string;
    href?: string;
    menus: { name: string; href?: string; target?: string }[];
  }[];
}>();

const router = useRouter();
const appState = useAppState();
const mouseHandler = (index: number, val: boolean) => {
  if (index === 0) {
    appState.value.menuIsActive = val;
  }
};

const shopList = [
  { name: "京东", link: "https://mall.jd.com/index-1000480161.html" },
  { name: "天猫", link: "https://shenmoushuma.tmall.com" },
];

// 商城跳转
function handleShopClick(item: any) {
  window.open(item.link, "_blank");
}
// 导航栏

function handleNavClick(item: any) {
  router.push(item.href);
}
</script>

<template>
  <div class="page-header-container">
    <header class="page-header">
      <div class="header-container flex items-center">
        <a class="logo-wrap flex-none" href="/">
          <img src="@/assets/images/logo.png" class="w-full" />
        </a>

        <div class="flex-auto px-5vw">
          <div class="header-nav flex justify-around">
            <template v-for="(item, index) in navList" :key="index">
              <template v-if="index === 0">
                <div class="relative nav-item">
                  <div
                    :class="['h-full cursor-pointer']"
                    @mouseenter="mouseHandler(index, true)"
                    @mouseout="mouseHandler(index, false)"
                  >
                    {{ item.name }}
                  </div>
                </div>
              </template>

              <template v-else>
                <Dropdown :list="item.menus" @command="handleNavClick">
                  <div class="nav-item">
                    <div class="h-full cursor-pointer">
                      {{ item.name }}
                    </div>
                  </div>
                </Dropdown>
              </template>
            </template>
          </div>
        </div>

        <!-- <SearchBar /> -->

        <Dropdown
          :list="shopList"
          class="xl:ml-12.5 ml-6"
          @command="handleShopClick"
        >
          <div class="flex-center h-54px">
            <div class="shop-entry">商城</div>
          </div>
        </Dropdown>
      </div>
    </header>

    <div v-show="appState.fillAppHeader" class="h-[var(--pc-header-height)]">
      <!-- FillHeader -->
    </div>
  </div>

  <!-- S 产品弹窗 -->
  <PcMenu />
  <!-- E 产品弹窗 -->
</template>

<style lang="less">
.header-nav {
  .nav-item {
    padding: 0 1.5vw;
    height: var(--pc-header-height);
    line-height: var(--pc-header-height);
    color: #fff;
    font-size: 20px;
    white-space: nowrap;
  }

  &-container {
    --height: 520px;
    position: fixed;
    top: 54px;
    left: 0;
    width: 100%;
    height: 0;
    background: #fff;
    transition-duration: 500ms;
    overflow: hidden;
    transition-delay: 70ms;
    z-index: 10;

    &:hover,
    &.is-active {
      height: var(--height);
    }
  }

  &-menu {
    max-width: var(--maxWidth);
    margin: 0 auto;
    padding: 40px 0;

    .menu {
      display: flex;

      &-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 30px;
        font-size: 18px;
        &:first-child {
          margin-top: 0;
        }
      }
    }
  }
}

.shop-entry {
  display: block;
  width: 84px;
  height: 25px;
  line-height: 25px;
  border-radius: 20px;
  background: rgba(121, 72, 234, 1);
  /** 文本1 */
  font-size: 12px;
  color: rgba(255, 255, 255, 1);
  box-shadow: none;
  border: none;
  text-align: center;
}
</style>
